<template>
  <div class="gl_contentBox gl_heightFull">
    <div class="gl_searchBox gl_mb_15">
      <el-row :gutter="20">
        <el-col :span="4">
          <p>搜索学员</p>
          <el-input v-model="selectedMsg.Filter" placeholder="请输入学员姓名\联系方式" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="4">
          <p>状态</p>
          <el-select v-model="selectedMsg.IsStarted" size="mini" placeholder="预约状态" filterable @change="getPageList()">
            <el-option v-for="item in appointmentStatus" :key="item.code" :value="item.code" :label="item.value" />
          </el-select>
        </el-col>
        <el-col :span="5.5">
          <p>上课时间</p>
          <el-date-picker
            v-model="chooseTime"
            size="mini"
            style="width:100%"
            type="daterange"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="handleTimeChange"
          />
        </el-col>
        <el-col :span="4">
          <p>预约课程</p>
          <el-input v-model="selectedMsg.CourseName" placeholder="请输入课程名称" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="4">
          <p>上课老师</p>
          <el-input v-model="selectedMsg.CoachName" placeholder="请输入老师姓名" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="2.5" class="search_btn">
          <!-- 查询按钮 -->
          <el-button type="primary" size="mini" @click="getPageList()">筛选</el-button>
          <!-- 重置按钮 -->
          <el-button type="warning" size="mini" @click="reset()">重置</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="gl_mb_15">
      <el-button type="primary" size="mini" @click="newAppointment()">新建预约</el-button>
    </div>
    <el-table v-loading="loading" class="gl_table_noBorder" :data="tableData" size="mini">
      <el-table-column label="学员姓名">
        <template slot-scope="scope">
          {{ scope.row.registerStudent ? scope.row.registerStudent.name : '--' }}
        </template>
      </el-table-column>
      <el-table-column label="联系方式">
        <template slot-scope="scope">
          <div v-if="scope.row.registerStudent && scope.row.registerStudent.studentContacts && scope.row.registerStudent.studentContacts.length > 0">
            <div v-for="item in scope.row.registerStudent.studentContacts" :key="item.id">{{ item.phoneNumber ? item.phoneNumber : '--' }}</div>
          </div>
          <div v-else>--</div>
        </template>
      </el-table-column>
      <el-table-column label="预约课程">
        <template slot-scope="scope">
          {{ scope.row.courseSchedule && scope.row.courseSchedule.courseName ? scope.row.courseSchedule.courseName : '' }}
        </template>
      </el-table-column>
      <el-table-column label="上课时间" :min-width="150">
        <template slot-scope="scope">
          <div v-if="scope.row.courseSchedule && scope.row.courseSchedule.courseDate">
            <span>{{ scope.row.courseSchedule.courseDate | FormatDate('yyyy-MM-dd') }}</span>
            <span>
              {{ 10 > scope.row.courseSchedule.startHour ? '0' + scope.row.courseSchedule.startHour : scope.row.courseSchedule.startHour }}:{{ 10 > scope.row.courseSchedule.startMinute ? '0' + scope.row.courseSchedule.startMinute : scope.row.courseSchedule.startMinute }} -
              {{ 10> scope.row.courseSchedule.endHour ? '0' + scope.row.courseSchedule.endHour : scope.row.courseSchedule.endHour }}:{{ 10 > scope.row.courseSchedule.endMinute ? '0' + scope.row.courseSchedule.endMinute : scope.row.courseSchedule.endMinute }}
            </span>
          </div>
          <div v-else>--</div>
        </template>
      </el-table-column>
      <el-table-column label="上课老师">
        <template slot-scope="scope">
          <div v-if="scope.row.courseSchedule && scope.row.courseSchedule.courseEmployees && scope.row.courseSchedule.courseEmployees.length > 0">
            <div v-for="item in scope.row.courseSchedule.courseEmployees" :key="item.id">{{ item.courseEmployeeTypeCode === 'Coach' ? item.employee.name : '' }}</div>
          </div>
          <div v-else>--</div>
        </template>
      </el-table-column>
      <el-table-column label="教室">
        <template slot-scope="scope">
          {{ scope.row.courseSchedule && scope.row.courseSchedule.classroomName ? scope.row.courseSchedule.classroomName : '' }}
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          {{ scope.row.courseSchedule ? scope.row.courseSchedule.isFinished === true ? '已上课' : '待上课' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="预约来源">
        <template slot-scope="scope">
          {{ scope.row.reservationSource && scope.row.reservationSource.code === 'ParentReservation' ? '家长端' : '后台管理端' }}
        </template>
      </el-table-column>
      <el-table-column label="预约时间" :min-width="130">
        <template slot-scope="{row}">
          <span v-if="row.creationTime">
            {{ row.creationTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" fixed="right">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="clickDelete(row)">取消</el-button>
          <el-button size="mini" type="text" @click="clickEdit(row)">新增</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row class="gl_mt_15" style="text-align: right;">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="selectedMsg.MaxResultCount"
        :current-page="selectedMsg.Page"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-row>
    <choose-students ref="chooseStudents" />
  </div>
</template>

<script>
import chooseStudents from './chooseStudents.vue';
import {
  Api_GET_ClubReservationQO // 获取预约列表
} from '@/lib/form/hall/swaggerForm';
import {
  api_GET_ClubReservation, // 获取预约列表
  api_DELETE_ClubReservationId // 取消预约
} from '@/extend/api/hallswaggerApi/app';
export default {
  components: { chooseStudents },
  data() {
    return {
      total: 0,
      selectedMsg: new Api_GET_ClubReservationQO(),
      appointmentStatus: [{ code: '', value: '全部状态' }, { code: true, value: '已上课' }, { code: false, value: '待上课' }],
      chooseTime: [],
      loading: false,
      tableData: []
    };
  },
  mounted() {
    if (this.selectedMsg) {
      this.selectedMsg.IsStarted = '';
      this.selectedMsg.CourseName = '';
      this.selectedMsg.status = '';
    }
    this.getPageList();
  },
  methods: {
    // 获取数据
    getPageList(page) {
      this.selectedMsg.Page = page || 1;
      this.selectedMsg.SkipCount = (this.selectedMsg.Page - 1) * this.selectedMsg.MaxResultCount;
      this.selectedMsg.IncludeStudent = true;
      this.selectedMsg.IncludeStudentContacts = true;
      this.selectedMsg.IncludeClass = true;
      this.selectedMsg.IncludeCourse = true;
      this.selectedMsg.IncludeCoach = true;
      this.selectedMsg.IncludeClassroom = true;
      this.selectedMsg.IncludeDeleted = true;
      this.selectedMsg.IncludeReservationSource = true;
      this.loading = true;
      api_GET_ClubReservation(this.selectedMsg).then(res => {
        this.loading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickEdit(row) {
      const _name = 'SelectAppointmentClass';
      this.$router.push({
        name: _name,
        query: {
          studentId: row.registerStudentId
        }
      });
    },
    // 新建预约
    newAppointment() {
      this.$refs.chooseStudents.getPageList();
      this.$refs.chooseStudents.dialogVisible = true;
    },
    // 取消预约
    clickDelete (row) {
      const that = this;
      this.$confirm(`您确定要取消预约吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          that.loading = true;
          api_DELETE_ClubReservationId(row.id).then(res => {
            that.loading = false;
            this.getPageList();
            this.$message({ type: 'success', message: '取消成功' });
          }, error => {
            that.loading = false;
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          });
        })
        .catch(() => {
        });
    },
    reset() {
      this.selectedMsg = new Api_GET_ClubReservationQO();
      this.selectedMsg.IsStarted = '';
      this.selectedMsg.CourseName = '';
      this.selectedMsg.status = '';
      this.chooseTime = [];
      this.getPageList();
    },
    handleTimeChange(time) {
      this.selectedMsg.StartDateTime = time ? time[0] : null;
      this.selectedMsg.EndDateTime = time ? time[1] : null;
      this.getPageList();
    },
    // 分页
    changePage(page) {
      this.getPageList(page);
    },
    changePageSize(pagesize) {
      this.selectedMsg.MaxResultCount = pagesize;
      this.getPageList();
    }
  }
};
</script>

<style scoped>
</style>
